package org.agile2soft.nexoma.tapestry.components;

import java.util.List;

import org.agile2soft.nexoma.core.controllers.ServiceController;
import org.agile2soft.nexoma.core.controllers.ServiceOptionController;
import org.agile2soft.nexoma.core.entities.Service;
import org.agile2soft.nexoma.core.entities.ServiceOption;
import org.agile2soft.nexoma.tapestry.model.SessionContainer;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

@IncludeJavaScriptLibrary({"ServiceMenuViewer.js"})
public class ServiceMenuViewer {

    @Property
    private Service currentService;

    @Property
    private ServiceOption currentOption;

    @SessionState
    @Property
    private SessionContainer session;

    @Inject
    private ServiceController serviceController;

    public List<Service> getServices() {
        return serviceController.findByGroup(session.getCurrentGroup());
    }

    public List<ServiceOption> getOptions() {
        return serviceOptionController.findByService(currentService);
    }

    public boolean isServiceActive() {
        return null!=session.getCurrentService();
    }

    public boolean isOptionActive() {
        return null!=session.getCurrentOption();
    }

    public boolean isServiceSelected() {
        return (isServiceActive()) &&
               session.getCurrentService().getId().equals(currentService.getId());
    }

    public boolean isOptionSelected() {
        return (isOptionActive()) &&
               session.getCurrentOption().getId().equals(currentOption.getId());
    }

    public String getServiceClass() {
        return ( isServiceSelected() ) ? "menu-service-selected"
                                       : "menu-service";
    }

    public String getOptionClass() {
        return ( isServiceSelected() && isOptionSelected() ) ? "menu-option-selected":"menu-option";
    }

    @Inject
    private RenderSupport renderSupport;

    @Inject
    private ComponentResources componentResources;

    @BeginRender
    void beginRender() {
        allocatedClientId = renderSupport.allocateClientId(componentResources);
    }

    @AfterRender
    void afterRender() {
        renderSupport.addInit("menuviewer", allocatedClientId);
    }

    private String allocatedClientId;

    public String getClientId() {
        return  allocatedClientId;
    }

    @OnEvent(component="selectService")
    Object doSelectService(Integer id) {
        session.setCurrentService(serviceController.findById(id));
        session.clearOption();
        return session.getCurrentService().getUrl();
    }

    @Inject
    private ServiceOptionController serviceOptionController;

    @OnEvent(component="selectOption")
    Object doSelectOption(Integer id) {
        ServiceOption option = serviceOptionController.findById(id);
        session.setCurrentService(option.getService());
        session.setCurrentOption(option);
        return option.getUrl();
    }


}